package com.logistics.distribution;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.logistics.database.Distributiondatabase;

public class Stockqueryservlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Stockqueryservlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Distributiondatabase distributiondatabase = new Distributiondatabase();
		//System.out.println(request.getParameter("storeroom_name"));
		//System.out.println(request.getParameter("product_name"));
		String store_name = URLDecoder.decode(request.getParameter("storeroom_name"),"utf-8");
		String product_name = URLDecoder.decode(request.getParameter("product_name"),"utf-8");
		int storeroom_id = distributiondatabase.getStoreroom_idbystoreroom_name(store_name);
		int product_id = distributiondatabase.getProduct_idbyproduct_name(product_name);
		//System.out.println("storeroom_id:"+storeroom_id);
		//System.out.println("product_id:"+product_id);
		
		int sum_of_allot = getSumofallot(product_id,storeroom_id);
		int sum_of_save = getSumofsave(product_id,storeroom_id);
		int sum_of_return = getSumofprereturn(product_id,storeroom_id);
		int sum_of_all = sum_of_allot+sum_of_save+sum_of_return;
		
		String result = sum_of_all+","+sum_of_allot+","+sum_of_save+","+sum_of_return;
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		//System.out.println(result);
		response.getWriter().write(result);
		response.getWriter().close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("post");
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
	public int getSumofprereturn(int product_id,int storeroom_id)
	{
		int result=0;
		Distributiondatabase distributiondatabase = new Distributiondatabase();
		ResultSet resultSet=distributiondatabase.getStock("where product_id = \'"+product_id+"\' and storeroom_id = \'"+storeroom_id+"\' and product_type = \'pre_return\'");
		try {
			if(resultSet.next())
			{
				result = resultSet.getInt("product_number");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
		
	}
	public int getSumofsave(int product_id,int storeroom_id)
	{
		int result=0;
		Distributiondatabase distributiondatabase = new Distributiondatabase();
		ResultSet resultSet=distributiondatabase.getStock("where product_id = \'"+product_id+"\' and storeroom_id = \'"+storeroom_id+"\' and product_type = \'save\'");
		try {
			if(resultSet.next())
			{
				result = resultSet.getInt("product_number");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(result);
		return result;
		
	}
	public int getSumofallot(int product_id,int storeroom_id)
	{
		int result=0;
		Distributiondatabase distributiondatabase = new Distributiondatabase();
		ResultSet resultSet=distributiondatabase.getStock("where product_id = \'"+product_id+"\' and storeroom_id = \'"+storeroom_id+"\' and product_type = \'allot\'");
		try {
			if(resultSet.next())
			{
				result = resultSet.getInt("product_number");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(result);
		return result;
		
	}
}
